﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.Statistics.NM;
using DevExpress.XtraEditors.Controls;
using System.Threading;
using MINERP.BLL.Statistics;

namespace MINERP.Statistics.Forms.NM
{
    public partial class FrmSalesStat : Form
    {
        public FrmSalesStat()
        {
            InitializeComponent();
        }
        NM_ShopBLL _bll;
        SalesStatBll _currBill;
        private List<Shop_Info> _shopList;
        private void radioButtonAreas_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonAreas.Checked == true)
            {
                List<string> strAreaList = _shopList.Select(s => s.Area).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }
        }

        private void radioButtonSaleMan_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonSaleMan.Checked)
            {
                List<string> strAreaList = _shopList.Select(s => s.Salesman).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }
        }
        private void BindShopList(List<Shop_Info> shopList)
        {
            cmkShop.Properties.DataSource = shopList.OrderBy(s => s.ShopName).ToList();
            cmkShop.Properties.DisplayMember = "ShopName";
            cmkShop.Properties.ValueMember = "ShopId";
            cmkShop.Properties.SelectAllItemCaption = "全选";
            cmkShop.SelectAll();
        }
        private void InitilControl()
        {
            _shopList = _bll.GetAllShop();
            if (radioButtonAreas.Checked)
            {
                List<string> strAreaList = _shopList.Select(s => s.Area).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }
            else
            {
                List<string> strAreaList = _shopList.Select(s => s.Salesman).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }

            BindShopList(_shopList);
        }
        bool _isRun = false;
        int _SelectType = 0;
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.btnOK.Enabled = false;
            progressBar1.Minimum = 0;

            int icount = 0;
            foreach (CheckedListBoxItem item in cmkArea.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    icount++;
                }
            }
            progressBar1.Maximum = icount;
            this.Cursor = Cursors.WaitCursor;
            if (_isRun == true)
            {
                MessageBox.Show("正在统计，请稍后");
                return;
            }
            _isRun = true;
            this.Cursor = Cursors.WaitCursor;
            if (dtStart.Value > dtEnd.Value)
            {
                MessageBox.Show("起始日期必须小于结束日期");
                dtStart.Value = dtEnd.Value;
                this.Cursor = Cursors.Default;
                return;
            }


            Thread thMake = new Thread(DoWork);
            thMake.IsBackground = true;
            thMake.Start();

            this.Cursor = Cursors.Default;
            _SelectType = comboBox1.SelectedIndex;
        }
        void DoWork()
        {

            DateTime dateStart = dtStart.Value;
            DateTime dateEnd = dtEnd.Value;

            if (_shopList != null && _shopList.Count > 0)
            {
                List<int> selectedShopList = new List<int>();//被选择的商户列表

                foreach (CheckedListBoxItem item in cmkShop.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        selectedShopList.Add(Convert.ToInt32(item.Value));
                    }
                }
                List<ShopStatsInfo> listPM;
                if (_SelectType == 0)
                {
                     listPM = _currBill.GetStatsProduct(selectedShopList, dateStart, dateEnd);
                     DisplayView(listPM,1);

                }
                else
                {
                    listPM = new List<ShopStatsInfo>();
                    var groupResult=_currBill.GetStatsProduct(selectedShopList, dateStart, dateEnd).GroupBy(g=>g.ShopName);
                    foreach (var item in groupResult)
                    {
                        ShopStatsInfo ssi = new ShopStatsInfo();
                        ssi.ShopName = item.Key;
                        ssi.OtherMoeny = item.Sum(it=>it.OtherMoeny);
                        ssi.PrintAreas = item.Sum(it => it.PrintAreas);
                        ssi.PrintMoney = item.Sum(it => it.PrintMoney);
                        //ssi.ProductDate = item.Sum(it => it.PrintAreas);
                        ssi.ProductMoney = item.Sum(it => it.ProductMoney);
                        ssi.AllMoney = item.Sum(it => it.AllMoney);
                        listPM.Add(ssi);
                    }
                    DisplayView(listPM, 2);
                }
               
            }
        }
        delegate void deleteDisplayView(List<ShopStatsInfo> listPM,int type);
        void DisplayView(List<ShopStatsInfo> listPM,int type)
        {
            if (!this.InvokeRequired)
            {
                if (type == 1)
                {
                    gridControlProduct.MainView = gridViewProductDetail;
                    gridControlProduct.DataSource = listPM;
                    gridControlProduct.RefreshDataSource();
                }
                else
                {
                    gridControlProduct.MainView = ViewProduct;
                    gridControlProduct.DataSource = listPM;
                    gridControlProduct.RefreshDataSource();
                }
                label2.Text = "请将计算结果导出并保存";
                _isRun = false;
                this.btnOK.Enabled = true;
            }
            else
            {
                this.BeginInvoke(new deleteDisplayView(DisplayView), new object[] { listPM, type });
            }
        }
        private static FrmSalesStat instance = null;
        public static FrmSalesStat GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmSalesStat();

                }
                return instance;
            }
        }
        private void FrmSalesStat_Load(object sender, EventArgs e)
        {
            _bll = new NM_ShopBLL();
            _currBill = new SalesStatBll();
            _currBill.ShowCurrPointHandle += new SalesStatBll.ShowProcess(_currBill_ShowCurrPointHandle);
            _currBill.ShowMsgHandle += new SalesStatBll.ShowMessage(_currBill_ShowMsgHandle);
            _shopList = _bll.GetAllShop();
            if (radioButtonAreas.Checked)
            {
                List<string> strAreaList = _shopList.Select(s => s.Area).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }
            else
            {
                List<string> strAreaList = _shopList.Select(s => s.Salesman).Distinct().ToList();
                List<AreaInfo> areaList = new List<AreaInfo>();
                foreach (var s in strAreaList)
                {
                    areaList.Add(new AreaInfo() { AreaName = s });
                }
                dtStart.Value = DateTime.Now.Date.Date;
                dtEnd.Value = DateTime.Now.Date.Date;
                cmkArea.Properties.DataSource = areaList;
                cmkArea.Properties.DisplayMember = "AreaName";
                cmkArea.Properties.ValueMember = "AreaName";
                cmkArea.Properties.SelectAllItemCaption = "全选";
                cmkArea.SelectAll();
            }

            BindShopList(_shopList);
            comboBox1.SelectedIndex = 0;
        }

        private void cmkArea_EditValueChanged(object sender, EventArgs e)
        {
            //根据地区显示商户
            if (radioButtonAreas.Checked)
            {
                List<string> areaList = new List<string>();
                foreach (CheckedListBoxItem item in cmkArea.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        areaList.Add(item.Value.ToString());
                    }
                }
                if (_shopList != null && _shopList.Count > 0)
                {
                    BindShopList(_shopList.Where(s => areaList.Contains(s.Area)).ToList());
                }
            }
            else
            {
                List<string> areaList = new List<string>();
                foreach (CheckedListBoxItem item in cmkArea.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        areaList.Add(item.Value.ToString());
                    }
                }
                if (_shopList != null && _shopList.Count > 0)
                {
                    BindShopList(_shopList.Where(s => areaList.Contains(s.Salesman)).ToList());
                }
            }
        }
        delegate void DisPalyMSG(string str);
        void _currBill_ShowMsgHandle(string str)
        {
            if (!this.InvokeRequired)
            {
                label2.Text = str;
            }
            else
            {
                this.BeginInvoke(new DisPalyMSG(_currBill_ShowMsgHandle), new object[] { str });
            }
        }
        delegate void DisPalyProgress(int intCurr);
        void _currBill_ShowCurrPointHandle(int currInt)
        {
            if (!this.InvokeRequired)
            {
                progressBar1.Value = currInt;
            }
            else
            {
                this.BeginInvoke(new DisPalyProgress(_currBill_ShowCurrPointHandle), new object[] { currInt });
            }
        }

        private void simpleButtonExport_Click(object sender, EventArgs e)
        {

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //saveFileDialog1.FileName = cmbGroup.Text+"组的员工业绩.xls";

                string saveDir = saveFileDialog1.FileName;
                if (!string.IsNullOrEmpty(saveDir))
                {

                    gridControlProduct.MainView.ExportToXls(saveDir);
                    MessageBox.Show("导出成功!");
                }
            }
        }

        private void FrmSalesStat_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

    }


    public class AreaInfo
    {
        public string AreaName { get; set; }
    }
}
